//
//  ExpressionViewController.m
//  Expressions
//
//  Created by admin on 3/10/13.
//  Copyright (c) 2013 admin. All rights reserved.
//

#import "ExpressionViewController.h"
#import "DetailViewController.h"
#import "AboutMeViewController.h"

@interface ExpressionViewController ()

@end

@implementation ExpressionViewController
@synthesize tableView = _tableView;
@synthesize subjectList, iconImages,bannerIsVisible;
@synthesize banerView = _banerView;

-(void)dealloc{
    
    [subjectList release];
    [iconImages release];
    [_tableView release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    //self.navigationItem.leftBarButtonItem = self.editButtonItem;
    
    // UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    //  self.navigationItem.rightBarButtonItem = addButton;
    //self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
    
    self.subjectList = [[[NSArray alloc]initWithObjects: @"Hình học",
                         @"Đại số",
                         @"Lượng giác",
                         @"Phương trình",
                         @"Hình học Giải tích",
                         @"Đạo hàm Vi phân",
                         @"Tích phân",
                         @"Về tác giả",nil]autorelease];;
    
    self.iconImages = [[[NSArray alloc]
                        initWithObjects:@"icon_HH.png",
                        @"icon_DS.png",
                        @"icon_LG.png",
                        @"icon_PhuongTrinh.png",
                        @"icon_GiaiTich.png",
                        @"icon_DaoHam.png",
                        @"icon_TichPhan.png",
                        @"logo_s.png",nil]autorelease];
    
    self.title= @"Công thức";
    self.banerView = [[[ADBannerView alloc]initWithFrame:CGRectMake(0, 460-50, _banerView.frame.size.width, _banerView.frame.size.height)]autorelease];
   
    _banerView.delegate =  self;
    _banerView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin ;
    if ([ExpressionUtils isLandscapse]) {
        _banerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
    }else{
        _banerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    }
    [self.view addSubview:_banerView];
    [self.view bringSubviewToFront:_banerView];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(BOOL)shouldAutorotate
{
    return YES;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return YES;
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    CGRect rect = self.view.bounds;
    [self.tableView setFrame:rect];
    if (_banerView) {
        if ([ExpressionUtils isLandscapse]) {
            _banerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
        }else{
            _banerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
        }
        rect = _banerView.frame;
        rect.origin.x = (self.view.frame.size.width - _banerView.frame.size.width)/2;
        rect.origin.y = self.view.frame.size.height - _banerView.frame.size.height;
        _banerView.frame = rect;

    }
}
#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return (subjectList.count +1);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell== nil){
        cell=[[UITableViewCell alloc] initWithStyle:
              UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
    }
    // Configure the cell...
    if (indexPath.row < subjectList.count) {
        cell.textLabel.text= [subjectList objectAtIndex:indexPath.row];
        
        cell.accessoryType= UITableViewCellAccessoryDisclosureIndicator;
        
        UIImage *iconPhoto = [UIImage imageNamed:
                              [self.iconImages objectAtIndex: [indexPath row]]];
        
        cell.imageView.image=iconPhoto;
    }else{
        cell.textLabel.text = @"";
        cell.imageView.image = nil;
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == ([_tableView numberOfRowsInSection:0] -2)) {
        AboutMeViewController *aboutViewController = [[AboutMeViewController alloc]initWithNibName:@"AboutMeViewController" bundle:nil];
        [self.navigationController pushViewController:aboutViewController animated:YES];
        [aboutViewController release];
    }else{
        DetailViewController * DVC = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
        NSIndexPath * path= [self.tableView indexPathForSelectedRow];
        NSString * theSubject = [subjectList objectAtIndex:path.row];
        DVC.subjectNumber = path.row;
        DVC.subjectName = theSubject;
        [self.navigationController pushViewController:DVC animated:YES];
    }
    
}
/*
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }*/


#pragma mark -
#pragma mark - AdBanerView Method
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    if(!self.bannerIsVisible)
    {
        
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1];
        [banner setAlpha:1];
        // Assumes the banner view is just off the bottom of the screen.
        //banner.frame = CGRectOffset(banner.frame, 0, self.view.frame.size.height-50);
//        banner.frame = CGRectOffset(banner.frame, 0,14);
        
        [UIView commitAnimations];
        
        self.bannerIsVisible = YES;
    }
    
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    if(!self.bannerIsVisible)
    {
        
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1];
        [banner setAlpha:0];
        // Assumes the banner view is just off the bottom of the screen.
        //banner.frame = CGRectOffset(banner.frame, 0, self.view.frame.size.height-50);
        //  banner.frame = CGRectOffset(banner.frame, 0, 50.0f);
        [UIView commitAnimations];
        
        self.bannerIsVisible = NO;
    }
}



/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
 {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */






@end
